package top.drinyeit.nongmao.presenter;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import java.util.List;
import java.util.Map;

import top.drinyeit.nongmao.activity.CommodityDetailNewActivity;
import top.drinyeit.nongmao.adapter.CategoryAdapter;
import top.drinyeit.nongmao.fragment.GoodsSearchResultFragment;
import top.drinyeit.nongmao.model.CategoryGoodsDetailsBean;
import top.drinyeit.nongmao.retrofit.HttpResponse;
import top.drinyeit.nongmao.retrofit.RetrofitMethod;
import top.drinyeit.nongmao.retrofit.subscriber.CommonSubscriber;
import top.drinyeit.nongmao.retrofit.subscriber.SubscriberListener;

import static top.drinyeit.nongmao.app.Constant.COMMON_SUCCESS;

/**
 * Created by wu on 2018/1/18.
 */

public class GoodsSearchResultPresenter {
    int p, s;
    private GoodsSearchResultFragment fragment;
    private CategoryAdapter categoryAdapter;

    public GoodsSearchResultPresenter(GoodsSearchResultFragment fragment) {
        this.fragment = fragment;
    }

    //商品搜索
    public void categoryGoodsSearch(final Map<String, Object> map) {
        final int index = (int) map.get("page_index");
        final int priceType = map.get("price_type") == null ? 0 : (int) map.get("price_type");
        final int selxlType = map.get("sell_type") == null ? 0 : (int) map.get("sell_type");
        p = priceType;
        s = selxlType;
        CommonSubscriber<HttpResponse<List<CategoryGoodsDetailsBean>>> subscriber = new CommonSubscriber<>(new SubscriberListener<HttpResponse<List<CategoryGoodsDetailsBean>>>() {
            @Override
            public void onError(String e, int code) {
                fragment.shSwipeRefreshLayout.finishLoadmore();
                fragment.shSwipeRefreshLayout.finishRefresh();
                fragment.showToastMsg("错误，请重新操作！");
            }

            @Override
            public void onNext(final HttpResponse<List<CategoryGoodsDetailsBean>> o) {
                fragment.shSwipeRefreshLayout.finishLoadmore();
                fragment.shSwipeRefreshLayout.finishRefresh();
                if (o.getCode() == COMMON_SUCCESS) {
                    if (p != priceType || s != selxlType) {
                        categoryAdapter = new CategoryAdapter(o.getData(),null);
                    }
                    fragment.pageIndex = index;
                    if (index == 1) {
                        if (categoryAdapter == null) {
                            if (o.getData() != null && o.getData().size() > 0) {
                                categoryAdapter = new CategoryAdapter(o.getData(),null);
                                fragment.mRecyclerView.setLayoutManager(new LinearLayoutManager(fragment.getActivity()));
                                fragment.mRecyclerView.setAdapter(categoryAdapter);
                            }
                        } else {
                            if (o.getData() == null || o.getData().size() == 0) {
                                categoryAdapter.clear();
                            } else {
                                categoryAdapter.setData(o.getData());
                            }
                            categoryAdapter.notifyDataSetChanged();
                        }
                    } else if (index > 1) {
                        if (o.getData() != null && o.getData().size() > 0) {
                            categoryAdapter.addData(o.getData());
                            categoryAdapter.notifyItemRangeInserted(categoryAdapter.getData().size(), o.getData().size());
                        } else {
                            fragment.showToastMsg("没有更多啦");
                        }
                    }
                    if (categoryAdapter != null && categoryAdapter.getData().size() > 0){
                        categoryAdapter.setOnItemClickListener(new CategoryAdapter.OnItemClickListener() {
                            @Override
                            public void onItemClick(View view, final int position) {
                                Intent intent = new Intent(fragment.getActivity(), CommodityDetailNewActivity.class);
                                intent.putExtra("id", categoryAdapter.getData().get(position).getId());
                                fragment.getActivity().startActivity(intent);
                            }
                        });
                    }
                } else {
                    fragment.showToastMsg(o.getMessage());
                }
            }
        });
        RetrofitMethod.getInstance().categorySearch(subscriber, map);
    }


}
